import { ScoreUI } from './UI/ScoreUI';
import { Player } from './Player';
import { _decorator, Component, Node, Prefab, instantiate, find, Vec2, v2, v3, Vec3, director, game, RigidBody2D, Collider2D, isValid, Widget, UITransform, BoxCollider2D, input, Input, Sprite, view, ResolutionPolicy, PolygonCollider2D, AudioClip } from 'cc';
const { ccclass, property } = _decorator;
import { AwardManager } from './AwardManager';
import { EDITOR_NOT_IN_PREVIEW } from 'cc/env';
import { BrickManager } from './BrickManager';
import { Bullet } from './Ball';
import { LevelManager } from './LevelManager';
import { resBallUI } from './UI/resBallUI';
import { toolsScript } from './toolsScript';
import { ScoreManager } from './ScoreManager';
import { AudioManager } from './AudioManager';


export enum GameDropAwardType {
    Wall,    // 城墙
    Ball,    // 球体
    Big,     // 球变大
    Small,    // 球变小
    Max
}

export enum GameState {
    Init,      // 准备阶段
    running,    // 正在运行
    Pause,     // 暂停
    Over,    // 结束
    Max
}

@ccclass('GameSceneUI')
export class GameSceneUI extends Component {
    private static instance: GameSceneUI = null;

    @property(Prefab)
    playerPrefab:Prefab = null;
    @property(Prefab)
    ballPrefab:Prefab = null;
    @property(ScoreUI)
    scoreUI:ScoreUI=null;

    @property(resBallUI)
    resBallUI:resBallUI=null;

    @property(Node)
    pausePanel: Node = null; // 暂停界面节点
    @property(Prefab)
    bottomWall:Prefab=null; //底部墙体
    @property(Node)
    overPanel: Node = null; // 结束界面节点
    @property(Prefab)
    passLevel:Prefab =null;//过关显示
    @property(Number)
    ballSpeed:number = 400; //球初始速度
    @property(Number)
    brickDropSpeed = 10; //10秒下落一次
    @property(Prefab)
    shootTips:Prefab = null; //发射提示
    @property(AudioClip)
    shootBGM:AudioClip = null; //发射音效
    @property(AudioClip)
    getItemBGM:AudioClip = null; //拾取道具音效
    @property(AudioClip)
    contacwallBGM:AudioClip = null; //球碰撞墙音效
    @property(AudioClip)
    contacBrickBGM:AudioClip = null; //球碰撞墙音效
    @property(AudioClip)
    gameFailBGM1:AudioClip = null; //低分失败音效
    @property(AudioClip)
    gameFailBGM2:AudioClip = null; //高分失败音效

    shootTipsNode:Node = null;
    pallLevelNode:Node = null;

    gameState:number = GameState.Init;

    playerPos:Vec3=v3(4,-433);
    ballPos:Vec3=v3(0.82,-136);

    fitCount:number=0;//已经击碎的砖块数量

    bottomWallInst:Node=null;// 底部墙体实例
    bottomWallInstTimer:number = 10000; // 底部墙体实例存在时间
    bottomWallInstBegionTimer:number = 0; // 底部墙体实例起始时间

    playerNode:Node = null;
    ballList:(Node | null)[] = new Array(10).fill(null); 

    Score:number = 0; //本局游戏得分
    topRecord:number = 0;//历史最高得分记录

    //击碎的砖块
    fitBricks:number[] = new Array(100).fill(0);

    //开始暂停的时间点
    pauseStartTimer:number = 0;

    TouchTimes:number = 0;
    TouchStartTime1:number = 0;
    TouchStartTime2:number = 0;

    //游戏进度，关卡数
    gameLevel:number = 1;
    isMovingBalls = false;

    //秒定时
    secondTimer:number = 0;
    //10秒定时
    tenSecondTimer:number = 0;

    bigScaleArray:number[] = [1,1.3,1.4,1.5,1.6,1.7,1.8];
    smallScaleArray:number[] = [1,0.8,0.7,0.6,0.5,0.4,0.3];
    bigScaleCount = 0; //变大次数
    smallScaleCount = 0;//变小次数
    playerInitScale = new Vec3();//玩家原始尺寸大小
    playerInitNodeScale = new Vec3();
    tempScale = new Vec3();

    testCount = 2;

    onLoad() {
        this.initViewAdapter();
        if (GameSceneUI.instance === null) 
        {
            GameSceneUI.instance = this;
        } else {
            console.error("GameSceneUI 已存在，请确保场景中只有一个 GameSceneUI");
            this.node.destroy();
        }

        input.on(Input.EventType.TOUCH_START,this.onTouchStart,this);
        input.on(Input.EventType.TOUCH_END,this.onTouchEnd,this);

        // 读取字符串
        let topRecordString:string = toolsScript.getItem('topRecord');
        console.log("get value : ",topRecordString,"lengh : ",topRecordString.length)
        if(topRecordString != null && topRecordString.length > 0)
        {
            this.topRecord = parseInt(topRecordString);
        }
    }

    protected onDestroy(){
        input.off(Input.EventType.TOUCH_MOVE,this.onTouchStart,this);
        input.off(Input.EventType.TOUCH_MOVE,this.onTouchEnd,this);
    }

    public reset() {
        // 重置单例类的状态
        GameSceneUI.instance = null;
    }

    public static getInstance(): GameSceneUI {
        return GameSceneUI.instance;
    }

    /**
     * getGameState
     */
    public getGameState():number {
        return this.gameState;
    }

     // 在游戏启动时调用
     initViewAdapter() {
        // 设置设计分辨率
        view.setDesignResolutionSize(1080, 1920, ResolutionPolicy.SHOW_ALL);
        
        // 根据实际屏幕比例动态调整
        const screenRatio = view.getVisibleSize().width / view.getVisibleSize().height;
        const designRatio = 1080 / 1920;
    
        if (screenRatio > designRatio) {
            // 宽屏设备（如 428×926），上下留黑边，宽度撑满
            view.setDesignResolutionSize(1080, 1920, ResolutionPolicy.FIXED_WIDTH);
        } else {
            // 窄屏设备，左右留黑边，高度撑满
            view.setDesignResolutionSize(1080, 1920, ResolutionPolicy.FIXED_HEIGHT);
        }
    }

    start() {
        // 启动定时器，每隔 moveInterval 秒移动一次
        this.secondTimer = new Date().getTime();
        
        this.scoreUI.updateUI(this.Score);

        //创建砖块
        BrickManager.getInstance().GameLoad();

        //创建球体的实例
        const ball = instantiate(this.ballPrefab);
        this.ballList[0] = ball;

        this.node.addChild(ball);
        ball.setPosition(this.playerPos);
        ball.active = false;

        //将球的实例传给player中，方便控制移动
        let ballMoudle =  ball.getComponent(Bullet);
        ballMoudle.changeSpeed(this.ballSpeed);

        //创建玩家的实例
        this.playerNode = instantiate(this.playerPrefab);

        this.node.addChild( this.playerNode);
        this.playerNode.setPosition(this.playerPos);

        const bodyNode = this.playerNode.getChildByName("Body");
        if(bodyNode)
        {
            // 获取 UITransform 组件
            const uiTransform = bodyNode.getComponent(UITransform);
            if (uiTransform) {
                // 设置原始大小
                this.playerInitScale.set(uiTransform.width,uiTransform.height,0);
            }
        }
        
        const playerCollider = this.playerNode.getComponent(PolygonCollider2D);
        if (playerCollider) {
            playerCollider.node.setScale(this.tempScale);
            this.playerInitNodeScale = this.playerNode.scale.clone();
        }


        //将球的实例传给player中，方便控制移动
        let playerMoudle =  this.playerNode.getComponent(Player);
        playerMoudle.SetBallPrefab(ball);

        if(this.shootTips)
        {
            if(this.shootTipsNode == null)
            {
                this.shootTipsNode = instantiate(this.shootTips);
            }
    
            this.node.addChild(this.shootTipsNode);
        }
        else
        {
            console.error("Create shootTips Fail");
        }
    }   

    //进入下一关
    gameEnterNextLevel()
    {
        if(this.pallLevelNode)
        {
            this.node.removeChild(this.pallLevelNode);
        }

        this.isMovingBalls = false;

        let FirstInit = true;
        //球归位
        for (let index = 0; index < this.ballList.length; index++) {
            const ball = this.ballList[index];
            if(ball)
            {
                if(FirstInit)
                {
                    //设置一个球跟随玩家
                    ball.active = true;
                    FirstInit = false;
                    let playerMoudle =  this.playerNode.getComponent(Player);
                    playerMoudle.SetBallPrefab(ball);
                    ball.setPosition(this.ballPos);
                }
                else
                {
                    ball.active = false;
                    ball.setPosition(0,0,0);
                }

                const ballCollider = ball.getComponent(Collider2D);
                if (ballCollider) {
                    ballCollider.enabled=false;
                }

                const ballRigidBody2D = ball.getComponent(RigidBody2D);
                if (ballRigidBody2D) {
                    ballRigidBody2D.enabled=false; 
                }

                const Ball_Node = ball.getComponent(Bullet);
                if(Ball_Node == null)
                {
                    console.error("GameSceneUI onTouchEnd Ball_Node is NULL !");
                    continue;
                }

                Ball_Node.changeShoot(false);
                Ball_Node.changeSpeed(this.ballSpeed*(1+(this.gameLevel-1)*0.02));
            }
        }

        //玩家归为
        this.playerNode.setPosition(this.playerPos);

        //创建砖块
        BrickManager.getInstance().GameLoad();

        if(this.shootTips)
        {
            if(this.shootTipsNode == null)
            {
                this.shootTipsNode = instantiate(this.shootTips);
            }
    
            this.node.addChild(this.shootTipsNode);
        }
        else
        {
            console.error("Create shootTips Fail");
        }

        this.gameState = GameState.Init;
    }

    update(deltaTime: number) {
        const nowTime = new Date().getTime();

        //秒计时
        if(nowTime - this.secondTimer >= this.brickDropSpeed*1000) 
        {   
            if(this.gameState == GameState.running)
            {
                BrickManager.getInstance().moveBricksDown();
            }
            
            this.secondTimer = nowTime;
        }       

        if(this.bottomWallInstBegionTimer != 0 )
        {
            if(this.gameState == GameState.Over || (nowTime - this.bottomWallInstBegionTimer-this.pauseStartTimer) > this.bottomWallInstTimer)
            {
                this.pauseStartTimer = 0;
                this.bottomWallInstBegionTimer = 0;
                // 延迟一帧删除
                this.scheduleOnce(() => {
                    if(isValid(this.bottomWallInst))
                    {
                        this.bottomWallInst.destroy();
                        this.bottomWallInst = null;
                    }
                });
            }
        }

        if(this.TouchTimes == 1)
        {
            if(nowTime - this.TouchStartTime1 > 350)
            {
                this.TouchTimes = 0;
                this.TouchStartTime1 = 0;
            }
        }

        this.updateBallPos();
    }

    onTouchStart()
    {
        this.TouchTimes += 1;
        if(this.TouchTimes == 1)
            this.TouchStartTime1 = new Date().getTime();
        else
            this.TouchStartTime2 = new Date().getTime();
    }

    onTouchEnd()
    {
        if(this.TouchTimes < 2)
        {
            return;
        }

        let cdtime = this.TouchStartTime2 - this.TouchStartTime1;
        if(cdtime <= 350 && (this.gameState == GameState.running || this.gameState == GameState.Init))
        {   
            LevelManager.getInstance().updateLevel(this.gameLevel);

            if(this.gameState != GameState.running)
            {
                BrickManager.getInstance().GameStart();
                this.resBallUI.updateUI(this.GetCurAllBallNum());
                this.node.removeChild(this.shootTipsNode);
            }
                
            this.resBallUI.node.active = true;
            this.gameState = GameState.running;

            let isShoot = false;

            for (let index = 0; index < this.ballList.length; index++) {
                const ball = this.ballList[index];
                if(ball)
                {
                    const ballsprite = ball.getComponent(Sprite);
                    if(ballsprite){
                        ballsprite.enabled = true;
                    }

                    ball.active = true;
                    this.scheduleOnce(() => {
                        if(isValid(ball))
                        {
                            ball.active = true;
                        }
                    });

                    this.node.addChild(ball);
                    const ballcollider = ball.getComponent(Collider2D);
                    if(ballcollider)
                    {
                        ballcollider.enabled = true;
                        ballcollider.body.wakeUp();//唤醒刚体
                    }
                    
                    const ballRigidBody2D = ball.getComponent(RigidBody2D);
                    if(ballRigidBody2D)
                    {
                        ballRigidBody2D.enabled = true;
                        ballRigidBody2D.wakeUp();
                    }

                    const Ball_Node = ball.getComponent(Bullet);
                    if(Ball_Node == null)
                    {
                        console.error("GameSceneUI onTouchEnd Ball_Node is NULL !");
                        continue;
                    }

                    if(Ball_Node.getShoot())
                    {
                        continue;
                    }

                    ball.setPosition(this.playerNode.position.x-40,this.playerNode.position.y,this.playerNode.position.z);
                    Ball_Node.changeBallGroupToPlayer();
                    Ball_Node.InitVelocity();
                    Ball_Node.changeSpeed(this.ballSpeed*(1+(this.gameLevel-1)*0.02));
                    Ball_Node.changeShoot(true);
                    Ball_Node.SetBallID(index);
                    isShoot = true;
                }
            }

            if(isShoot)
            {
                AudioManager.getInstance().playSFX(this.shootBGM);
            }
        }

        this.TouchTimes = 0;
        this.TouchStartTime1 = 0;
        this.TouchStartTime2 = 0;
    }

    // 添加点击 重新开始游戏 事件 
    public onResStartButtonClick()
    {
        AudioManager.getInstance().resetAllsfxPool();
        AwardManager.getInstance().reset();
        GameSceneUI.getInstance().reset();
        BrickManager.getInstance().reset();
        LevelManager.getInstance().reset();
        this.fitBricks.fill(0);
        //this.reset();
        director.resume();

        director.loadScene("02-GameScene"); 
        return;
    }

    //游戏结束
    public GameOver(iswin:boolean)
    {  
        
        if(this.gameState == GameState.Over)
        {
            return;
        }

        

        this.gameState = GameState.Over;
        this.fitCount = 0;
        this.fitBricks.fill(0);
        this.resBallUI.node.active = false;
        if(iswin)
        {
            this.gameLevel += 1;
            if(this.pallLevelNode == null)
            {
                this.pallLevelNode = instantiate(this.passLevel);
            }
            
            this.node.addChild(this.pallLevelNode);

            LevelManager.getInstance().removeNode();

            //延迟1侦
            this.scheduleOnce(() => {
                BrickManager.getInstance().GameOver();
            });

            //延迟1秒
            this.scheduleOnce(() => {
                this.moveBallsToPlayer();
            },1);

            //延迟5秒结束，让砖块被击碎的动画播完
            this.scheduleOnce(() => {
                //进入下一关
                this.gameEnterNextLevel();
            },5);
        }
        else
        {
            if(this.Score <= 50)
            {
                AudioManager.getInstance().playSFX(this.gameFailBGM1);
            }
            else
            {
                AudioManager.getInstance().playSFX(this.gameFailBGM2);
            }
            
            //延迟1秒结束，让砖块被击碎的动画播完
            this.scheduleOnce(() => {
                director.pause();
                this.overPanel.active = true;
                const topRecordNode = this.overPanel.getChildByName("topRecord");
                if(topRecordNode)
                {
                    const scoreManager = topRecordNode.getComponent(ScoreManager);
                    if(scoreManager)
                    {
                        scoreManager.gameOver();
                    }
                }
            },1);
        }

        if(this.Score > this.topRecord)
        {
             // 存储字符串
             toolsScript.setItem('topRecord', this.Score.toString());
        }
    }

    // 让所有 ball 向 playerNode 移动
    private moveBallsToPlayer() {
        this.isMovingBalls = true; // 开始移动 ball
        const targetPos = new Vec2(this.playerNode.position.x-40, this.playerNode.position.y+297);

        for (let index = 0; index < this.ballList.length; index++) {
            const ball = this.ballList[index];
            if (ball && isValid(ball)) {
                const ballsprite = ball.getComponent(Sprite);
                if(ballsprite)
                {
                    ballsprite.enabled = false;
                }

                const ballComponent = ball.getComponent(Bullet);
                if (ballComponent) {
                    ballComponent.changeBallGroupToPlayer();
                    // 设置 ball 的速度为向 playerNode 移动
                    const direction = new Vec2(
                        targetPos.x - ball.position.x,
                        targetPos.y - ball.position.y
                    ); // 计算方向向量并归一化 .normalize()

                    ballComponent.SetVelocity(direction); // 设置速度 .multiplyScalar(ballComponent.getSpeed())
                    ballComponent.changeSpeed(0.5);

                    const ballCollider = ball.getComponent(Collider2D);
                    if (ballCollider && ballCollider.enabled) {
                        ballCollider.enabled=false;
                    }

                    const ballRigidBody2D = ball.getComponent(RigidBody2D);
                    if (ballRigidBody2D && ballRigidBody2D.enabled) {
                        ballRigidBody2D.enabled=false;
                    }
                }
            }
        }
    }

    updateBallPos()
    {
        // 如果正在移动 ball，更新它们的位置
        if (this.isMovingBalls) {
            const targetPos = new Vec2(this.playerNode.position.x-40, this.playerNode.position.y+297);

            for (let index = 0; index < this.ballList.length; index++) {
                const ball = this.ballList[index];
                if (ball && isValid(ball)) {
                    const ballComponent = ball.getComponent(Bullet);
                    if (ballComponent) {
                        // 计算 ball 到 playerNode 的距离
                        const distance = Vec2.distance(
                            new Vec2(ball.position.x, ball.position.y),
                            targetPos
                        );

                        // 如果距离小于一定值，停止移动
                        if (distance < 5) { // 5 是一个阈值，可以根据需要调整
                            ballComponent.SetVelocity(Vec2.ZERO); // 停止移动
                            ball.setPosition(targetPos.x,targetPos.y,0); // 设置 ball 的位置为 playerNode 的位置
                        }
                    }
                }
            }
        }
    }

    /**
     * name
     */
    public FitBrick(ID:number) {
        AudioManager.getInstance().playSFX(this.contacBrickBGM);
        if(this.fitBricks[ID]==0)
        {
            this.fitBricks[ID] = 1;
            this.fitCount += 1;
            this.AddScore(1);
            if(this.fitCount >= BrickManager.getInstance().getAllBrickNum())
            {
                this.GameOver(true);
            }
        }
    }

    public AddScore(addscore:number)
    {
        this.Score += addscore;
        this.scoreUI.updateUI(this.Score);
    }

    public GetAward(awardType:number)
    {
        console.log("GetAward~~~~~~~~~~~~~~~~");
        AudioManager.getInstance().playSFX(this.getItemBGM);
        switch (awardType) {
            case GameDropAwardType.Ball://获得多个小球
                this.CreateMoreBall();
            break;
            case GameDropAwardType.Big://角色变大
                this.PlayerBig();
            break;
            case GameDropAwardType.Small://角色变小
                this.PlayerSmall();
            break;
            case GameDropAwardType.Wall://获得底部碰撞墙
                this.CreateBottomWall();
            break;
        
            default:
                break;
        }
    }

    PlayerBig(isSmall:boolean=false)
    {
        if(this.playerNode == null)
        {
            return;
        }

        if(isSmall == false && this.smallScaleCount > 0)
        {
            this.PlayerSmall(true);
            return;
        }

        if(isSmall)
        {
            if(this.bigScaleCount <= 0)
            {
                console.error("player is smaller ,can not change to small !");
                return;
            }
            this.bigScaleCount -= 1;
            
        }
        else
        {
            if(this.bigScaleCount >= this.bigScaleArray.length-1)
            {
                console.log("player is bigger ,can not change to big !");
                return;//不在继续变大
            }

            this.bigScaleCount += 1;
            console.log("big count : ",this.bigScaleCount);
        }

        const currentScale = this.bigScaleArray[this.bigScaleCount];
        // 重置节点缩放，再应用新值
        this.playerNode.setScale(Vec3.ONE);
        this.tempScale.set(currentScale, currentScale, 1);
        this.playerNode.setScale(this.tempScale);

        console.log("Big Scale : ",this.bigScaleArray[this.bigScaleCount]," Big Count : ",this.bigScaleCount);

        const playerCollider = this.playerNode.getComponent(PolygonCollider2D);
        if (playerCollider) {
            playerCollider.node.setScale(this.tempScale);
            playerCollider.apply();
        }
    }

    PlayerSmall(isBig:boolean=false)
    {
        if(this.playerNode == null)
        {
            return;
        }

        if(isBig == false && this.bigScaleCount > 0)
        {
            this.PlayerBig(true);
            return;
        }

        if(isBig)
        {
            if(this.smallScaleCount <= 0)
            {
                console.error("player is bigger ,can not change to big !");
                return;
            }
            this.smallScaleCount -= 1;
            
        }
        else
        {
            if(this.smallScaleCount >= this.smallScaleArray.length-1)
            {
                console.log("player is smaller ,can not change to small !");
                return;//不在继续变小
            }

            this.smallScaleCount += 1;
            console.log("small count : ",this.smallScaleCount);
        }

        const currentScale = this.smallScaleArray[this.smallScaleCount];
        // 重置节点缩放，再应用新值
        this.playerNode.setScale(Vec3.ONE);
        this.tempScale.set(currentScale, currentScale, 1);
        this.playerNode.setScale(this.tempScale);

        console.log("Samll Scale : ",this.smallScaleArray[this.smallScaleCount]," Small Count : ",this.smallScaleCount);

        
        const playerCollider = this.playerNode.getComponent(PolygonCollider2D);
        if (playerCollider) {
            playerCollider.node.setScale(this.tempScale);
            playerCollider.apply();
        }

        
    }

    CreateBottomWall()
    {
        if(this.bottomWallInst == null)
        {
            this.bottomWallInst = instantiate(this.bottomWall);
            //this.bottomWallInst.setSiblingIndex(3);
            const boxCollider2D = this.bottomWallInst.getComponent(BoxCollider2D);

            // 1. 添加 Widget 组件
            const widget = this.bottomWallInst.addComponent(Widget);
            widget.target = this.bottomWallInst.parent;
            // 3. 配置水平对齐
            widget.isAlignHorizontalCenter = true;    // 水平居中对齐
            widget.horizontalCenter = 0;             // 相对中心点的偏移量
            // 使用上对齐模式
            widget.isAlignBottom = true;          // 底部对齐
            widget.bottom = 0;                   // 下边距 20 像素

            const uiTransform = this.node.getComponent(UITransform);
            const uiSize = uiTransform.contentSize;
            
            let childNode = this.bottomWallInst.getChildByName('地墙');

            const wallFatherUITransform = this.bottomWallInst.getComponent(UITransform);
            const walluiTransform = childNode.getComponent(UITransform);
            

            const wallManager = this.node.getChildByName('WallManager');
            const rightWall = wallManager.getChildByName('right');
            const leftWall = wallManager.getChildByName('left');
            const rightWallTransform = rightWall.getComponent(UITransform);
            const leftWallTransform = leftWall.getComponent(UITransform);

            wallFatherUITransform.setContentSize(uiSize.width+rightWallTransform.width,uiSize.height*0.147);//
            walluiTransform.setContentSize(wallFatherUITransform.contentSize);
            boxCollider2D.size.width = wallFatherUITransform.contentSize.width;
            boxCollider2D.size.height = wallFatherUITransform.contentSize.height;
            boxCollider2D.offset.x = 0;
            boxCollider2D.offset.y = 0;
            this.node.addChild(this.bottomWallInst);
            



            this.bottomWallInst.setPosition(leftWallTransform.width,-363,0);
            this.bottomWallInstBegionTimer = new Date().getTime();
        }
    }

    GetCurAllBallNum():number
    {
        let num = 0;
        for (let index = 0; index < this.ballList.length; index++) {
            if(this.ballList[index] != null)
                num += 1;
        }

        return num;
    }

    CreateMoreBall()
    {
        //创建球体的实例
        for (let index = 0; index < this.ballList.length; index++) {
            if (this.ballList[index] != null && isValid(this.ballList[index])) 
            {
                continue;
            }

            const newNode = instantiate(this.ballPrefab);
            this.ballList[index] = newNode;

            this.resBallUI.updateUI(this.GetCurAllBallNum());
            return;
        }
    }

    public BallDestory(ballID:number)
    {
        this.ballList[ballID] = null;

        this.resBallUI.updateUI(this.GetCurAllBallNum());

        for (let index = 0; index < this.ballList.length; index++) {
            if(this.ballList[index] != null)
            {
                return; //至少还有一个球，继续运行
            }
        }

        //一个球都没有了，游戏失败
        this.GameOver(false);
    }

    public getPlayerPosY():number {
        return this.playerPos.y;
    }

    public getCavasPosY():number
    {
        return this.node.position.y;
    }

    //碰撞音效
    public playerContacAudio()
    {
        AudioManager.getInstance().playSFX(this.contacwallBGM);
    }

    //暂停游戏
    onPauseButtonClick()
    {
        if(this.gameState != GameState.running)
        {
            return;
        }
        this.gameState = GameState.Pause;

        this.pauseStartTimer = new Date().getTime()
        director.pause();
        this.pausePanel.active = true;
    }

    //继续游戏
    onResumeButtonClick()
    {
        if(this.gameState != GameState.Pause)
        {
            return;
        }

        if(this.bottomWallInstBegionTimer == 0)
        {
            this.pauseStartTimer = 0;
        }
        else
        {
            this.pauseStartTimer = new Date().getTime() - this.pauseStartTimer;//计算出暂停了多久
        }
        
        this.gameState = GameState.running;


        this.pausePanel.active = false;
        director.resume();
    }
}


